<?php

/**
 * Description of IndexController
 *
 * @author Admin
 */
class Category_IndexController extends Zend_Controller_Action
{
    protected $_model;
    protected $_authService;

    public function init()
    {
        $layout = $this->_helper->layout();
        $layout->setLayout('main');

        $this->_model = new Category_Model_Category();

        $this->_authService = new Main_Service_Authentication();

        // add forms
        $this->view->categoryform = $this->getCategoryForm();
    }

    /**
     * shows the list of all the catagories
     */
    public function indexAction()
    {
         // checking if user is logged in
        if(!$this->_authService->getAuth()->hasIdentity())
            $this->_redirect('/');

         $this->view->categories = $this->_model->getAllCategories();
    }

    public function newAction()
    {
        if(!$this->_authService->getAuth()->hasIdentity())
            $this->_redirect('/');
        
        $this->_helper->FlashMessenger('')->clearMessages();
        $this->_helper->FlashMessenger('')->setNamespace('header')->addMessage('New Category');
        $this->_forward('edit');
    }

    /**
     * Used to show the category edit/create form
     */
    public function editAction()
    {
         if(!$this->_authService->getAuth()->hasIdentity())
            $this->_redirect('/');
         
        $requst_params = $this->getRequest()->getParams();
      
        if( isset($requst_params['id']) && $requst_params['id'] !='' )
        {
            $this->_helper->FlashMessenger('')->clearMessages();
            $this->_helper->FlashMessenger('')->setNamespace('header')->addMessage('Edit Category');
            $id  =  $requst_params['id'];
            $this->view->category = $this->_model->getCategoryById($id);

            $this->view->categoryform = $this->getCategoryform()->populate( $this->view->category->toArray() );
        }
    }

    public  function saveAction()
    {
        // echo '<pre>'; print_r($_REQUEST); echo '</pre>';
        $request = $this->getRequest();
        if (!$request->isPost()) {
            return $this->_helper->redirector('index');
        }
       
        if (false === $this->_model->saveCategory($request->getPost())  )
        {
            $this->_helper->FlashMessenger('')->setNamespace('error')->addMessage('An error Occured');

            return $this->render('edit');
        }

        $this->_helper->FlashMessenger('')->setNamespace('success')->addMessage('Category info saved');
        return $this->_redirect('/category/index/');
    }


    public function deleteAction()
    {
        if(!$this->_authService->getAuth()->hasIdentity())
            $this->_redirect('/');

         if (false === ($id = $this->_getParam('id',false))) {
            throw new WOW_Exception('Unknown category');
        }

        if($this->_model->deleteCategory($id))
            $this->_helper->FlashMessenger('')->setNamespace('success')->addMessage('Category Deleted');
        else
            $this->_helper->FlashMessenger('')->setNamespace('Error')->addMessage('An error occured');

        return $this->_redirect('/category/index/');

    }

    public function getCategoryform()
    {
        return $this->_forms['categoryform'] = $this->_model->getForm('CategoryCategoryform');
    }
}
?>